//
//  Learner.m
//  Do Not Disturb
//
//  Created by Chris Karr on 9/15/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "Learner.h"


@implementation Learner

- (Learner *) init
{
	if (self = [super init])
		classCount = [[NSMutableDictionary alloc] init];
		
	return self;
}

- (void) reset
{
	[classCount release];

	classCount = [[NSMutableDictionary alloc] init];
}

- (NSNumber *) exampleCount
{
	int count = 0;
	NSString * key;
	NSEnumerator * iter = [[classCount allKeys] objectEnumerator];
	while (key = [iter nextObject])
		count += [[classCount objectForKey:key] intValue];
		
	return [NSNumber numberWithInt:count];
}

- (void) addExample:(NSArray *) features forClass:(NSString *) label
{
	NSNumber * count = [classCount objectForKey:label];
	
	if (count == nil)
		count = [NSNumber numberWithInt:0];
	
	[classCount setObject:[NSNumber numberWithInt:([count intValue] + 1)] forKey:label];
}

- (NSString *) getLabelForExample:(NSArray *) features
{
	float maxValue = 0.0;
	NSString * maxLabel = @"";
	
	NSArray * keys = [classCount allKeys];
	
	NSString * key;
	NSEnumerator * iter = [keys objectEnumerator];
	
	while (key = [iter nextObject])
	{
		if ([[classCount objectForKey:key] floatValue] > maxValue)
		{
			maxValue = [[classCount objectForKey:key] floatValue];
			maxLabel = key;
		}
	}
	
	return maxLabel;
}

@end
